package org.homeworkteam.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.homeworkteam.bean.Users;
import org.homeworkteam.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper, Users> {

    @Autowired
    private UserMapper userMapper;
    public int addUser(Users users) {
        return baseMapper.addUser(users);
    }
    public Users getUserInfo(Users users) {
        return baseMapper.getUserInfo(users);
    }

    public Users checkUser(String userAccount) {
        return baseMapper.checkUser(userAccount);
    }

    public boolean setUser(Users user) {
        // 创建更新条件
        UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", user.getUserId()); // 根据 user_id 更新

        // 执行更新操作
        int rows = userMapper.update(user, updateWrapper);
        return rows > 0; // 返回是否更新成功

    }

    /**
     * 根据用户 ID 列表查询用户 ID 和姓名
     *
     * @param userIds 用户 ID 列表
     * @return 用户 ID 和姓名列表
     */
    public List<Users> getUserNamesByIds(List<Long> userIds) {
        if (userIds == null || userIds.isEmpty()) {
            return null;
        }

        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_id", "user_name") // 只查询 user_id 和 user_name
                .in("user_id", userIds);

        return userMapper.selectList(queryWrapper);
    }
}
